package com.zm.admin.zmeng.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zm.admin.annotation.SysLogTag;
import com.zm.admin.utils.ResultBean;
import com.zm.admin.zmeng.domain.ZmVisitor;
import com.zm.admin.zmeng.dto.VisitorDto;
import com.zm.admin.zmeng.mapper.ZmVisitorMapper;
import com.zm.admin.zmeng.service.ZmVisitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/***
 *
 * @Auther: MuYun
 * @Date: 2022/12/8 10:59
 * @Description： 访客监控相关接口
 *
 */
@RequestMapping("/visit")
@RestController
public class VisitController {

    private static Logger logger= LoggerFactory.getLogger(LoginController.class);

    @Resource
    private ZmVisitorMapper zmVisitorMapper;

    @Resource
    private ZmVisitService zmVisitService;

    @RequestMapping("/list")
    public ResultBean list(VisitorDto visitorDto){
        logger.info("来看看参数:{}",visitorDto);
        ResultBean resultBean=new ResultBean();
        Integer pageNo=1;
        Integer pageSize=10;
        try{
            if(ObjectUtil.isNotEmpty(visitorDto.getPageNo())){
                pageNo= visitorDto.getPageNo();
            }
            if(ObjectUtil.isNotEmpty(visitorDto.getPageSize())){
                pageSize= visitorDto.getPageSize();
            }
            QueryWrapper<ZmVisitor> wrapper = designQueryMapper(visitorDto);
            Page<ZmVisitor> visitorPage = zmVisitService.page(new Page<>(pageNo, pageSize),wrapper);
            resultBean.setData(visitorPage).setCode(ResultBean.CODE_SUCCESS);
        }catch (Exception e){
            logger.error("查询访客列表接口报错：{}",e);
            resultBean.setCode(ResultBean.CODE_ERROR);
        }
        return resultBean;
    }

    @RequestMapping("/del")
    @SysLogTag(desc = "删除访问记录")
    public ResultBean del(@RequestBody List<ZmVisitor> list){
        ResultBean resultBean=new ResultBean();
        try{
            List<String> collect = list.stream().map(ZmVisitor::getId).collect(Collectors.toList());
            zmVisitorMapper.deleteBatchIds(collect);
            resultBean.setCode(ResultBean.CODE_SUCCESS);
        }catch (Exception e){
            logger.error("删除访问记录接口:{}",e);
            resultBean.setCode(ResultBean.CODE_ERROR);
        }
        return resultBean;
    }

    /**
     * 制造查询条件构造器
     * @param visitorDto
     * @return
     */
    private static QueryWrapper<ZmVisitor> designQueryMapper(VisitorDto visitorDto){
        QueryWrapper<ZmVisitor> wrapper=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(visitorDto.getIp())){
            wrapper.lambda().like(ZmVisitor::getIp,visitorDto.getIp());
        }
        if(StrUtil.isNotEmpty(visitorDto.getCity())){
            wrapper.lambda().like(ZmVisitor::getCity,visitorDto.getCity());
        }
        if(StrUtil.isNotEmpty(visitorDto.getPlatformType())){
            wrapper.lambda().like(ZmVisitor::getPlatformType,visitorDto.getPlatformType());
        }
        if(StrUtil.isNotEmpty(visitorDto.getStartTime())){
            wrapper.lambda().ge(ZmVisitor::getCreateTime,visitorDto.getStartTime());
        }
        if(StrUtil.isNotEmpty(visitorDto.getEndTime())){
            wrapper.lambda().le(ZmVisitor::getCreateTime,visitorDto.getEndTime());
        }
        return wrapper;
    }
}
